package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.WithdrawalSubsidiaryAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.WithdrawalDetailBean;
import com.huifaapp.mall.bean.WithdrawalTotalAmounctBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.EndLessOnScrollListener;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/1/4.
 */

public class WithdrawalSubsidiaryActivity extends AppCompatActivity {
    private RecyclerView acyvivty_withdrawRecyclerView;
    private SwipeRefreshLayout swipRefreshLayout;
    private WithdrawalSubsidiaryAdapter withdrawalSubsidiaryAdapter;
    private String DETAILURL= ApiManager.BASE_NIUPAI_URL+"/users/pay/detail";
    private  String TOTALAMOUNCTURL=ApiManager.BASE_NIUPAI_URL+"/users/totalAmounct";
    private List<WithdrawalDetailBean.DataBean.WithdrawListBean> withdrawListBeanList;
    private TextView acyvivty_withdrawTotalAmount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawasubsidiary);
        initView();
        initTotalAmounctHttp();
        initDetailHttp();
    }

    private void initTotalAmounctHttp() {
        Map<String,String>TotalAmounctMap=new HashMap<>();
        TotalAmounctMap.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        TotalAmounctMap.put("appId", AUniqueIdentifier.getDeviceId(WithdrawalSubsidiaryActivity.this));
        OkHttpUtils.post().params(TotalAmounctMap).url(TOTALAMOUNCTURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                 Gson gson=new Gson();
                WithdrawalTotalAmounctBean.DataBean dataBean = gson.fromJson(response, WithdrawalTotalAmounctBean.DataBean.class);
                acyvivty_withdrawTotalAmount.setText(dataBean.getTotalAmount()+"");
            }
        });
    }

    private void initDetailHttp() {
        Map<String,String>ParamsMap=new HashMap<>();
        ParamsMap.put("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        ParamsMap.put("appId",AUniqueIdentifier.getDeviceId(WithdrawalSubsidiaryActivity.this));
        ParamsMap.put("type","003");
        ParamsMap.put("start","1");
        ParamsMap.put("length","2");
        OkHttpUtils.post().params(ParamsMap).url(DETAILURL).build().execute(new StringCallback() {



            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                Log.e("QQQQQQQQQQQQQQQQQQQ",response);
                Gson gson = new Gson();
                WithdrawalDetailBean withdrawalDetailBean = gson.fromJson(response, WithdrawalDetailBean.class);
                withdrawListBeanList = new ArrayList<WithdrawalDetailBean.DataBean.WithdrawListBean>();
                withdrawListBeanList.addAll(withdrawalDetailBean.getData().getWithdrawList());
                withdrawalSubsidiaryAdapter = new WithdrawalSubsidiaryAdapter(WithdrawalSubsidiaryActivity.this,withdrawListBeanList);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WithdrawalSubsidiaryActivity.this, LinearLayoutManager.VERTICAL, false);
                        acyvivty_withdrawRecyclerView.setLayoutManager(linearLayoutManager);
                        acyvivty_withdrawRecyclerView.setAdapter(withdrawalSubsidiaryAdapter);
                        acyvivty_withdrawRecyclerView.addOnScrollListener(new EndLessOnScrollListener(linearLayoutManager) {
                            @Override
                            public void onLoadMore(int currentPage) {
                                boolean visBottom = WithdrawalSubsidiaryActivity.isVisBottom(acyvivty_withdrawRecyclerView);
                                if (visBottom==false){
                                }

                            }
                        });
                    }
                });
            }
        });
    }



    private void initView() {
        acyvivty_withdrawRecyclerView = (RecyclerView) findViewById(R.id.acyvivty_withdrawRecyclerView);
        acyvivty_withdrawTotalAmount = (TextView) findViewById(R.id.acyvivty_withdrawTotalAmount);


    }

//    private void iodMoreData() {
//        for (int i=0;i<10;i++){
//            WithdrawalList.add(WithdrawalList.get(i));
//            withdrawalSubsidiaryAdapter.notifyDataSetChanged();
//        }
//    }

    public static boolean isVisBottom(RecyclerView recyclerView){
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //屏幕中最后一个可见子项的position
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        //当前屏幕所看到的子项个数
        int visibleItemCount = layoutManager.getChildCount();
        //当前RecyclerView的所有子项个数
        int totalItemCount = layoutManager.getItemCount();
        //RecyclerView的滑动状态
        int state = recyclerView.getScrollState();
        if(visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && state == recyclerView.SCROLL_STATE_IDLE){
            return true;
        }else {
            return false;
        }
    }

}
